package com.hbm.usercenter.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStripExtends;
import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseActivityCommon;
import com.hbm.usercenter.fragment.OrderAllFragment;
import com.hbm.usercenter.fragment.OrderAlreadPayFragment;
import com.hbm.usercenter.fragment.OrderHalfPayFragment;
import com.hbm.usercenter.fragment.OrderToPayFragment;
import com.hbm.usercenter.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyOrderActivity extends BaseActivityCommon {


    @Bind(R.id.main_viewPager)
    ViewPager mMainViewPager;
    @Bind(R.id.tabs)
    PagerSlidingTabStripExtends mTabs;
    private String[] mOrderTitles;
    private List<Fragment> mFragmentList;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        initToolbar("我的订单");
    }

    @Override
    protected void initData() {
        mFragmentList = new ArrayList<Fragment>();
        OrderAllFragment orderAllFragment = new OrderAllFragment();
        mFragmentList.add(orderAllFragment);
        OrderToPayFragment orderToPayFragment = new OrderToPayFragment();
        mFragmentList.add(orderToPayFragment);
        OrderHalfPayFragment orderHalfPayFragment = new OrderHalfPayFragment();
        mFragmentList.add(orderHalfPayFragment);
        OrderAlreadPayFragment orderAlreadPayFragment = new OrderAlreadPayFragment();
        mFragmentList.add(orderAlreadPayFragment);

        mOrderTitles = UIUtils.getStringArr(R.array.order_titles);
        //        OrderAdapter adapter = new OrderAdapter();
        OrderFragmentStatePagerAdapter adapter = new OrderFragmentStatePagerAdapter
                (getSupportFragmentManager());
        mMainViewPager.setAdapter(adapter);
        mTabs.setViewPager(mMainViewPager);
    }

    class OrderFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

        public OrderFragmentStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            if (mOrderTitles != null) {
                return mOrderTitles.length;
            }
            return 0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mOrderTitles[position];
        }
    }

    class OrderAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (mOrderTitles != null) {
                return mOrderTitles.length;
            }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView tv = new TextView(UIUtils.getContext());
            tv.setText(mOrderTitles[position]);
            container.addView(tv);

            return tv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return mOrderTitles[position];
        }
    }

}
